package com.iplant.ams.serviceImpl;

import com.iplant.ams.service.AMSService;
import com.iplant.ams.service.basicenum.ams.AMSFixedAssetsType;
import com.iplant.ams.service.po.ams.*;
import com.iplant.ams.service.po.ams.asset.AMSAssetClassification;
import com.iplant.ams.service.po.ams.asset.AMSAssetInfo;
import com.iplant.ams.service.po.ams.asset.AMSAssetStandardModel;
import com.iplant.ams.service.po.ams.asset.AMSAssetStorage;
import com.iplant.ams.service.po.ams.fxt.AMSFrockTool;
import com.iplant.ams.service.po.ams.fxt.AMSMeasureTool;
import com.iplant.ams.service.po.ams.fxt.DMSFrockLedger;
import com.iplant.ams.service.po.dms.DMSDeviceLedger;
import com.iplant.ams.service.po.dms.DMSDeviceType;
import com.iplant.ams.serviceImpl.dao.ams.*;
import com.iplant.base.po.EnumItem;
import com.iplant.base.po.OutResult;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.po.enums.BaseEnum;
import com.iplant.base.po.enums.ExceptionTypes;
import com.iplant.base.po.excel.ExcelData;
import com.iplant.base.po.excel.ExcelLineData;
import com.iplant.base.po.excel.ExcelSheetData;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sql.orm.ORMUtils;
import com.iplant.base.utils.sql.orm.PoService;
import com.iplant.basic.service.po.bms.BMSClassInfo;
import com.iplant.basic.service.po.bms.BMSEmployee;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class AMSServiceImpl implements AMSService {
    private static Logger logger = LoggerFactory.getLogger(AMSServiceImpl.class);

    public AMSServiceImpl() {

    }

    private static boolean AMSAssetClassEnumInited = false;

    private static AMSService Instance = null;

    public static AMSService getInstance() {
        if (Instance == null)
            Instance = new AMSServiceImpl();

        return Instance;
    }

    //region POService
    PoService<DMSFrockLedger> mDMSFrockLedgerService = ORMUtils.GetPoService(DMSFrockLedger.class);
    PoService<AMSAssetInfo> mAssetInfoService = ORMUtils.GetPoService(AMSAssetInfo.class);
    PoService<AMSFrockTool> mAMSFrockToolService = ORMUtils.GetPoService(AMSFrockTool.class);
    PoService<DMSDeviceLedger> mDMSDeviceLedgerService = ORMUtils.GetPoService(DMSDeviceLedger.class);
    PoService<BMSClassInfo> mBMSClassInfoService = ORMUtils.GetPoService(BMSClassInfo.class);
    PoService<BMSEmployee> mBMSEmployeeService = ORMUtils.GetPoService(BMSEmployee.class);
    //endregion

    //region 供应商信息

    /**
     * 新增或修改供应商信息
     *
     * @param wLoginUser
     * @param wItem
     * @return
     */
    @Override
    public ServiceResult<Integer> AMS_UpdateSupplierInfo(BMSEmployee wLoginUser, AMSSupplierInfo wItem) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);

        try {
            OutResult<Integer> wErrorCode = new OutResult<>(Integer.valueOf(0));


            wResult.Result = AMSSupplierInfoDAO.getInstance().Update(wLoginUser, wItem, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception ex) {
            wResult.FaultCode += ex.toString();
            logger.error(ex.toString());
        }

        return wResult;
    }

    /**
     * 批量导入供应商信息
     *
     * @param wLoginUser
     * @param wItemList
     * @return
     */
    @Override
    public ServiceResult<Integer> AMS_ImportSupplierInfoList(BMSEmployee wLoginUser, List<AMSSupplierInfo> wItemList) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);

        try {
            OutResult<Integer> wErrorCode = new OutResult<>(Integer.valueOf(0));

            wResult = AMSSupplierInfoDAO.getInstance().Import(wLoginUser, wItemList, wErrorCode);
            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception ex) {
            wResult.FaultCode += ex.toString();
            logger.error(ex.toString());
        }

        return wResult;
    }

    /**
     * 删除单条供应商信息
     *
     * @param wLoginUser
     * @param wItem
     * @return
     */
    @Override
    public ServiceResult<Integer> AMS_DeleteSupplierInfo(BMSEmployee wLoginUser, AMSSupplierInfo wItem) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);

        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);

            AMSSupplierInfoDAO.getInstance().Delete(wLoginUser, wItem, wErrorCode);


//            ORMUtils.GetPoService(AMSSupplierInfo.class).DeletePO(wLoginUser, wItem, wErrorCode);
//            ORMUtils.GetPoService(AMSSupplierInfo.class).SelectPOList(wLoginUser, new HashMap<String, Object>(),
//                    Pagination.getDefault(), wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception ex) {
            wResult.FaultCode += ex.toString();
            logger.error(ex.toString());
        }

        return wResult;
    }

    /**
     * 批量删除供应商信息
     *
     * @param wLoginUser
     * @param wIDList
     * @return
     */
    @Override
    public ServiceResult<Integer> AMS_DeleteSupplierInfoList(BMSEmployee wLoginUser, List<Integer> wIDList) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);

        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);

            AMSSupplierInfoDAO.getInstance().DeleteList(wLoginUser, wIDList, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception ex) {
            wResult.FaultCode += ex.toString();
            logger.error(ex.toString());
        }

        return wResult;
    }

    /**
     * 条件查询供应商信息
     *
     * @param wLoginUser
     * @param wIDs
     * @param wName
     * @param wCode
     * @param wCreatorID
     * @param wActive
     * @param wContactName
     * @param wContactNumber
     * @param wOpeningBank
     * @param wPaymentAccount
     * @param wPagination
     * @return
     */
    @Override
    public ServiceResult<List<AMSSupplierInfo>> AMS_GetAMSSupplierInfoList(
            BMSEmployee wLoginUser, List<Integer> wIDs, String wName, String wCode, int wCreatorID, int wActive,
            String wContactName, String wContactNumber, String wOpeningBank, String wPaymentAccount,
            Calendar wStartTime, Calendar wEndTime, Pagination wPagination) {
        ServiceResult<List<AMSSupplierInfo>> wResult = new ServiceResult<>();

        try {
            OutResult<Integer> wErrorCode = new OutResult<>(Integer.valueOf(0));

            wResult.Result = AMSSupplierInfoDAO.getInstance().SelectList(
                    wLoginUser, wIDs, wName, wCode, wCreatorID, wActive,
                    wContactName, wContactNumber, wOpeningBank, wPaymentAccount,
                    wStartTime, wEndTime, wPagination, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception ex) {
            wResult.FaultCode += ex.toString();
            logger.error(ex.toString());
        }

        return wResult;
    }

    //endregion

    //region 采购申请单
    @Override
    public ServiceResult<Integer> AMS_UpdatePurchaseRequest(BMSEmployee wLoginUser,
                                                            AMSPurchaseRequest wItem) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);

        try {
            OutResult<Integer> wErrorCode = new OutResult<>(Integer.valueOf(0));

            wResult.Result = AMSPurchaseRequestDAO.getInstance().Update(
                    wLoginUser, wItem, wErrorCode);
            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception ex) {
            wResult.FaultCode += ex.toString();
            logger.error(ex.toString());
        }

        return wResult;
    }

    @Override
    public ServiceResult<Integer> AMS_DeletePurchaseRequest(BMSEmployee wLoginUser, AMSPurchaseRequest wItem) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);

        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);

            AMSPurchaseRequestDAO.getInstance().Delete(wLoginUser, wItem, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception ex) {
            wResult.FaultCode += ex.toString();
            logger.error(ex.toString());
        }

        return wResult;
    }

    @Override
    public ServiceResult<Integer> AMS_DeletePurchaseRequestList(BMSEmployee wLoginUser,
                                                                List<Integer> wIDList) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);

        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);

            AMSPurchaseRequestDAO.getInstance().DeleteList(wLoginUser, wIDList, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception ex) {
            wResult.FaultCode += ex.toString();
            logger.error(ex.toString());
        }

        return wResult;
    }

    @Override
    public ServiceResult<List<AMSPurchaseRequest>> AMS_GetPurchaseRequestList(BMSEmployee wLoginUser,
                                                                              List<Integer> wIDList,
                                                                              List<Integer> wStateIDList,
                                                                              Integer wActive, String wReceiverName,
                                                                              String wReceiverPhone,
                                                                              Calendar wStartTime, Calendar wEndTime,
                                                                              Pagination wPagination) {
        ServiceResult<List<AMSPurchaseRequest>> wResult = new ServiceResult<>();

        try {
            OutResult<Integer> wErrorCode = new OutResult<>(Integer.valueOf(0));

            wResult.Result = AMSPurchaseRequestDAO.getInstance().AMS_QueryPurchaseRequestList(
                    wLoginUser, wIDList, null, null, -1,
                    wActive, wStateIDList, wReceiverName, wReceiverPhone,
                    wStartTime, wEndTime, wPagination, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception ex) {
            wResult.FaultCode += ex.toString();
            logger.error(ex.toString());
        }
        return wResult;
    }

    //endregion

    //region 采购申请单物品详情

    @Override
    public ServiceResult<Integer> AMS_UpdatePurchaseRequestItem(BMSEmployee wLoginUser, AMSPurchaseRequestItem wItem) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);

        try {
            OutResult<Integer> wErrorCode = new OutResult<>(Integer.valueOf(0));

            wResult.Result = AMSPurchaseRequestItemDAO.getInstance().Update(wLoginUser, wItem, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception ex) {
            wResult.FaultCode += ex.toString();
            logger.error(ex.toString());
        }

        return wResult;
    }

    @Override
    public ServiceResult<Integer> AMS_DeletePurchaseRequestItem(BMSEmployee wLoginUser, AMSPurchaseRequestItem wItem) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);

        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            AMSPurchaseRequestItemDAO.getInstance().Delete(wLoginUser, wItem, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception ex) {
            wResult.FaultCode += ex.toString();
            logger.error(ex.toString());
        }

        return wResult;
    }

    @Override
    public ServiceResult<Integer> AMS_DeletePurchaseRequestItemList(BMSEmployee wLoginUser, List<Integer> wIDList) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);

        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            AMSPurchaseRequestItemDAO.getInstance().DeleteList(wLoginUser, wIDList, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception ex) {
            wResult.FaultCode += ex.toString();
            logger.error(ex.toString());
        }

        return wResult;
    }

    @Override
    public ServiceResult<List<AMSPurchaseRequestItem>> AMS_GetPurchaseRequestItemList(BMSEmployee wLoginUser,
                                                                                      int wRequestID,
                                                                                      Calendar wStartTime,
                                                                                      Calendar wEndTime,
                                                                                      Pagination wPagination) {
        ServiceResult<List<AMSPurchaseRequestItem>> wResult = new ServiceResult<>();

        try {
            OutResult<Integer> wErrorCode = new OutResult<>(Integer.valueOf(0));

            wResult.Result = AMSPurchaseRequestItemDAO.getInstance().SelectList(
                    wLoginUser, null, "", "", -1, -1, null,
                    wRequestID, wStartTime, wEndTime, wPagination, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception ex) {
            wResult.FaultCode += ex.toString();
            logger.error(ex.toString());
        }

        return wResult;
    }

    @Override
    public ServiceResult<List<AMSPurchaseRequestItem>> AMS_GetPurchaseRequestItemListNew(BMSEmployee wLoginUser,
                                                                                         int wRequestID,
                                                                                         Pagination wPagination) {
        ServiceResult<List<AMSPurchaseRequestItem>> wResult = new ServiceResult<>();

        try {
            OutResult<Integer> wErrorCode = new OutResult<>(Integer.valueOf(0));

            Map<String, Object> wParams = new HashMap<>();
            wParams.put("RequestID", wRequestID);

            wResult.Result = AMSPurchaseRequestItemDAO.getInstance().SelectPOList(wLoginUser, wParams,
                    AMSPurchaseRequestItem.class, wPagination, wErrorCode);
            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception ex) {
            wResult.FaultCode += ex.toString();
            logger.error(ex.toString());
        }

        return wResult;
    }

    //endregion

    //region 采购订单

    /**
     * 新增或修改采购订单
     *
     * @param wLoginUser
     * @param wItem
     * @return
     */
    @Override
    public ServiceResult<Integer> AMS_UpdatePurchaseOrder(BMSEmployee wLoginUser, AMSPurchaseOrder wItem) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);

        try {
            OutResult<Integer> wErrorCode = new OutResult<>(Integer.valueOf(0));

            wResult.Result = AMSPurchaseOrderDAO.getInstance().Update(wLoginUser, wItem, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception ex) {
            wResult.FaultCode += ex.toString();
            logger.error(ex.toString());
        }

        return wResult;
    }

    /**
     * 删除单条采购订单
     *
     * @param wLoginUser
     * @param wItem
     * @return
     */
    @Override
    public ServiceResult<Integer> AMS_DeletePurchaseOrder(BMSEmployee wLoginUser, AMSPurchaseOrder wItem) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);

        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);

            AMSPurchaseOrderDAO.getInstance().Delete(wLoginUser, wItem, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception ex) {
            wResult.FaultCode += ex.toString();
            logger.error(ex.toString());
        }

        return wResult;
    }

    /**
     * 批量删除采购订单
     *
     * @param wLoginUser
     * @param wIDList
     * @return
     */
    @Override
    public ServiceResult<Integer> AMS_DeletePurchaseOrderList(BMSEmployee wLoginUser, List<Integer> wIDList) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);

        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);

            AMSPurchaseOrderDAO.getInstance().DeleteList(wLoginUser, wIDList, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception ex) {
            wResult.FaultCode += ex.toString();
            logger.error(ex.toString());
        }

        return wResult;
    }

    /**
     * 条件查询采购订单
     *
     * @param wLoginUser
     * @param wIDs
     * @param wStateIDList
     * @param wActive
     * @param wSupplierID
     * @param wStartTime
     * @param wEndTime
     * @param wPagination
     * @return
     */
    @Override
    public ServiceResult<List<AMSPurchaseOrder>> AMS_GetPurchaseOrderList(BMSEmployee wLoginUser,
                                                                          List<Integer> wIDs, String wName,
                                                                          String wCode, Integer wCreatorID,
                                                                          List<Integer> wStateIDList, Integer wActive
            , Integer wSupplierID,
                                                                          Calendar wStartTime, Calendar wEndTime,
                                                                          Pagination wPagination) {
        ServiceResult<List<AMSPurchaseOrder>> wResult = new ServiceResult<>();

        try {
            OutResult<Integer> wErrorCode = new OutResult<>(Integer.valueOf(0));

            wResult.Result = AMSPurchaseOrderDAO.getInstance().SelectList(wLoginUser,
                    wIDs, wName, wCode, wCreatorID, wActive, wStateIDList, wSupplierID,
                    wStartTime, wEndTime, wPagination, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception ex) {
            wResult.FaultCode += ex.toString();
            logger.error(ex.toString());
        }

        return wResult;
    }

    //endregion

    //region 采购订单项

    /**
     * 新增或修改采购订单项
     *
     * @param wLoginUser
     * @param wItem
     * @return
     */
    @Override
    public ServiceResult<Integer> AMS_UpdatePurchaseOrderItem(BMSEmployee wLoginUser, AMSPurchaseOrderItem wItem) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);

        try {
            OutResult<Integer> wErrorCode = new OutResult<>(Integer.valueOf(0));

            wResult.Result = AMSPurchaseOrderItemDAO.getInstance().Update(wLoginUser, wItem, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception ex) {
            wResult.FaultCode += ex.toString();
            logger.error(ex.toString());
        }

        return wResult;
    }

    /**
     * 删除单条采购订单项
     *
     * @param wLoginUser
     * @param wItem
     * @return
     */
    @Override
    public ServiceResult<Integer> AMS_DeletePurchaseOrderItem(BMSEmployee wLoginUser, AMSPurchaseOrderItem wItem) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);

        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            AMSPurchaseOrderItemDAO.getInstance().Delete(wLoginUser, wItem, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception ex) {
            wResult.FaultCode += ex.toString();
            logger.error(ex.toString());
        }

        return wResult;
    }

    /**
     * 批量删除采购订单项
     *
     * @param wLoginUser
     * @param wItemList
     * @return
     */
    @Override
    public ServiceResult<Integer> AMS_DeletePurchaseOrderItemList(BMSEmployee wLoginUser,
                                                                  List<AMSPurchaseOrderItem> wItemList) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);

        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            AMSPurchaseOrderItemDAO.getInstance().DeleteList(wLoginUser, wItemList, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception ex) {
            wResult.FaultCode += ex.toString();
            logger.error(ex.toString());
        }

        return wResult;
    }

    /**
     * 条件查询采购订单项
     *
     * @param wLoginUser
     * @param wOrderID
     * @param wPurchaseRequestItemID
     * @param wAssetStandardModelID
     * @param wStartTime
     * @param wEndTime
     * @param wPagination
     * @return
     */
    @Override
    public ServiceResult<List<AMSPurchaseOrderItem>> AMS_GetPurchaseOrderItemList(BMSEmployee wLoginUser,
                                                                                  List<Integer> wIDList, int wOrderID
            , int wPurchaseRequestItemID, int wAssetStandardModelID,
                                                                                  Calendar wStartTime,
                                                                                  Calendar wEndTime,
                                                                                  Pagination wPagination) {
        ServiceResult<List<AMSPurchaseOrderItem>> wResult = new ServiceResult<>();

        try {
            OutResult<Integer> wErrorCode = new OutResult<>(Integer.valueOf(0));

            wResult.Result = AMSPurchaseOrderItemDAO.getInstance().SelectList(wLoginUser,
                    wIDList, "", "", -1, -1, null,
                    wOrderID, wPurchaseRequestItemID, wAssetStandardModelID,
                    wStartTime, wEndTime, wPagination, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception ex) {
            wResult.FaultCode += ex.toString();
            logger.error(ex.toString());
        }

        return wResult;
    }

    /**
     * 幺式注解法查询某采购订单下的订单项集合
     *
     * @param wLoginUser
     * @param wOrderID
     * @param wStartTime
     * @param wEndTime
     * @param wPagination
     * @return
     */
    @Override
    public ServiceResult<List<AMSPurchaseOrderItem>> AMS_GetPurchaseOrderItemListNew(BMSEmployee wLoginUser,
                                                                                     int wOrderID,
                                                                                     Calendar wStartTime,
                                                                                     Calendar wEndTime,
                                                                                     Pagination wPagination) {
        ServiceResult<List<AMSPurchaseOrderItem>> wResult = new ServiceResult<>();

        try {
            OutResult<Integer> wErrorCode = new OutResult<>(Integer.valueOf(0));

            Map<String, Object> wParams = new HashMap<>();
            wParams.put("OrderID", wOrderID);

            wResult.Result = AMSPurchaseOrderItemDAO.getInstance().SelectPOList(wLoginUser, wParams,
                    AMSPurchaseOrderItem.class, wPagination, wErrorCode);
            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception ex) {
            wResult.FaultCode += ex.toString();
            logger.error(ex.toString());
        }

        return wResult;
    }

    //endregion

    //region 收货单

    /**
     * 新增或修改收货单
     *
     * @param wLoginUser
     * @param wItem
     * @return
     */
    @Override
    public ServiceResult<Integer> AMS_UpdateReceiveGoods(BMSEmployee wLoginUser, AMSReceiveGoods wItem) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);

        try {
            OutResult<Integer> wErrorCode = new OutResult<>(Integer.valueOf(0));

            wResult.Result = AMSReceiveGoodsDAO.getInstance().Update(wLoginUser, wItem, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception ex) {
            wResult.FaultCode += ex.toString();
            logger.error(ex.toString());
        }

        return wResult;
    }

    /**
     * 删除单条收货单
     *
     * @param wLoginUser
     * @param wItem
     * @return
     */
    @Override
    public ServiceResult<Integer> AMS_DeleteReceiveGoods(BMSEmployee wLoginUser, AMSReceiveGoods wItem) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);

        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);

            AMSReceiveGoodsDAO.getInstance().Delete(wLoginUser, wItem, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception ex) {
            wResult.FaultCode += ex.toString();
            logger.error(ex.toString());
        }

        return wResult;
    }

    /**
     * 批量删除收货单
     *
     * @param wLoginUser
     * @param wIDList
     * @return
     */
    @Override
    public ServiceResult<Integer> AMS_DeleteReceiveGoodsList(BMSEmployee wLoginUser, List<Integer> wIDList) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);

        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);

            AMSReceiveGoodsDAO.getInstance().DeleteList(wLoginUser, wIDList, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception ex) {
            wResult.FaultCode += ex.toString();
            logger.error(ex.toString());
        }

        return wResult;
    }

    /**
     * 条件查询收货单
     *
     * @param wLoginUser
     * @param wIDList
     * @param wName
     * @param wCode
     * @param wCreatorID
     * @param wStateIDList
     * @param wActive
     * @param wOrderID
     * @param wSupplierID
     * @param wStartTime
     * @param wEndTime
     * @param wPagination
     * @return
     */
    @Override
    public ServiceResult<List<AMSReceiveGoods>> AMS_GetReceiveGoodsList(BMSEmployee wLoginUser,
                                                                        List<Integer> wIDList, String wName,
                                                                        String wCode, Integer wCreatorID,
                                                                        List<Integer> wStateIDList,
                                                                        Integer wActive, Integer wOrderID,
                                                                        Integer wSupplierID, Calendar wStartTime,
                                                                        Calendar wEndTime, Pagination wPagination) {
        ServiceResult<List<AMSReceiveGoods>> wResult = new ServiceResult<>();

        try {
            OutResult<Integer> wErrorCode = new OutResult<>(Integer.valueOf(0));

            wResult.Result = AMSReceiveGoodsDAO.getInstance().SelectList(wLoginUser,
                    wIDList, wName, wCode, wCreatorID, wActive, wStateIDList, wOrderID, wSupplierID,
                    wStartTime, wEndTime, wPagination, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception ex) {
            wResult.FaultCode += ex.toString();
            logger.error(ex.toString());
        }

        return wResult;
    }

    //endregion

    //region 收货单物品详情

    /**
     * 新增或修改收货单详情项
     *
     * @param wLoginUser
     * @param wItem
     * @return
     */
    @Override
    public ServiceResult<Integer> AMS_UpdateReceiveGoodsItem(BMSEmployee wLoginUser, AMSReceiveGoodsItem wItem) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);

        try {
            OutResult<Integer> wErrorCode = new OutResult<>(Integer.valueOf(0));

            wResult.Result = AMSReceiveGoodsItemDAO.getInstance().Update(wLoginUser, wItem, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception ex) {
            wResult.FaultCode += ex.toString();
            logger.error(ex.toString());
        }

        return wResult;
    }

    /**
     * 删除单条收货单详情
     *
     * @param wLoginUser
     * @param wItem
     * @return
     */
    @Override
    public ServiceResult<Integer> AMS_DeleteReceiveGoodsItem(BMSEmployee wLoginUser, AMSReceiveGoodsItem wItem) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);

        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);

            AMSReceiveGoodsItemDAO.getInstance().Delete(wLoginUser, wItem, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception ex) {
            wResult.FaultCode += ex.toString();
            logger.error(ex.toString());
        }

        return wResult;
    }

    /**
     * 批量删除收货单详情
     *
     * @param wLoginUser
     * @param wItemList
     * @return
     */
    @Override
    public ServiceResult<Integer> AMS_DeleteReceiveGoodsItemList(BMSEmployee wLoginUser,
                                                                 List<AMSReceiveGoodsItem> wItemList) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);

        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);

            AMSReceiveGoodsItemDAO.getInstance().DeleteList(wLoginUser, wItemList, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception ex) {
            wResult.FaultCode += ex.toString();
            logger.error(ex.toString());
        }

        return wResult;
    }

    /**
     * 条件查询收货单详情
     *
     * @param wLoginUser
     * @param wReceiveID
     * @param wOrderItemID
     * @param wStartTime
     * @param wEndTime
     * @param wPagination
     * @return
     */
    @Override
    public ServiceResult<List<AMSReceiveGoodsItem>> AMS_GetAMSReceiveGoodsItemList(BMSEmployee wLoginUser,
                                                                                   int wReceiveID, int wOrderItemID,
                                                                                   List<Integer> wStateList,
                                                                                   int wCreatorID,
                                                                                   Calendar wStartTime,
                                                                                   Calendar wEndTime,
                                                                                   Pagination wPagination) {
        ServiceResult<List<AMSReceiveGoodsItem>> wResult = new ServiceResult<>();

        try {
            OutResult<Integer> wErrorCode = new OutResult<>(Integer.valueOf(0));

            wResult.Result = AMSReceiveGoodsItemDAO.getInstance().SelectList(wLoginUser,
                    null, "", "", wCreatorID, -1,
                    wStateList, wReceiveID, wOrderItemID, wStartTime, wEndTime, wPagination, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception ex) {
            wResult.FaultCode += ex.toString();
            logger.error(ex.toString());
        }

        return wResult;
    }

    //endregion

    //region 资产入库单

    /**
     * 新增或修改入库单
     *
     * @param wLoginUser
     * @param wItem
     * @return
     */
    @Override
    public ServiceResult<Integer> AMS_UpdateAssetStorage(BMSEmployee wLoginUser, AMSAssetStorage wItem) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);

        try {
            OutResult<Integer> wErrorCode = new OutResult<>(Integer.valueOf(0));

            wResult.Result = AMSAssetStorageDAO.getInstance().Update(wLoginUser, wItem, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception ex) {
            wResult.FaultCode += ex.toString();
            logger.error(ex.toString());
        }

        return wResult;
    }

    /**
     * 删除单条入库单
     *
     * @param wLoginUser
     * @param wItem
     * @return
     */
    @Override
    public ServiceResult<Integer> AMS_DeleteAssetStorage(BMSEmployee wLoginUser, AMSAssetStorage wItem) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);

        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);

            AMSAssetStorageDAO.getInstance().Delete(wLoginUser, wItem, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception ex) {
            wResult.FaultCode += ex.toString();
            logger.error(ex.toString());
        }

        return wResult;
    }

    /**
     * 批量删除入库单
     *
     * @param wLoginUser
     * @param wIDList
     * @return
     */
    @Override
    public ServiceResult<Integer> AMS_DeleteAssetStorageList(BMSEmployee wLoginUser, List<Integer> wIDList) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);

        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);

            AMSAssetStorageDAO.getInstance().DeleteList(wLoginUser, wIDList, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception ex) {
            wResult.FaultCode += ex.toString();
            logger.error(ex.toString());
        }

        return wResult;
    }

    /**
     * 条件查询入库单
     *
     * @param wLoginUser
     * @param wIDs
     * @param wName
     * @param wCode
     * @param wCreatorID
     * @param wActive
     * @param wReceiveReceiptID
     * @param wSupplierID
     * @param wSourceID
     * @param wStateIDList
     * @param wStartTime
     * @param wEndTime
     * @param wPagination
     * @return
     */
    @Override
    public ServiceResult<List<AMSAssetStorage>> AMS_GetAssetStorageList(BMSEmployee wLoginUser,
                                                                        List<Integer> wIDs, String wName,
                                                                        String wCode, int wCreatorID, int wActive,
                                                                        Integer wReceiveReceiptID,
                                                                        Integer wSupplierID, Integer wSourceID,
                                                                        List<Integer> wStateIDList,
                                                                        Calendar wStartTime, Calendar wEndTime,
                                                                        Pagination wPagination) {
        ServiceResult<List<AMSAssetStorage>> wResult = new ServiceResult<>();

        try {
            OutResult<Integer> wErrorCode = new OutResult<>(Integer.valueOf(0));

            wResult.Result = AMSAssetStorageDAO.getInstance().SelectList(wLoginUser,
                    wIDs, wName, wCode, wCreatorID, wActive, wReceiveReceiptID, wSupplierID,
                    wSourceID, wStateIDList, wStartTime, wEndTime, wPagination, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception ex) {
            wResult.FaultCode += ex.toString();
            logger.error(ex.toString());
        }

        return wResult;
    }

    //endregion

    //region 资产列表

    /**
     * 新增或修改单条资产
     *
     * @param wLoginUser
     * @param wItem
     * @return
     */
    @Override
    public ServiceResult<Integer> AMS_UpdateAssetInfo(BMSEmployee wLoginUser, AMSAssetInfo wItem) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);

        try {
            OutResult<Integer> wErrorCode = new OutResult<>(Integer.valueOf(0));

            wResult.Result = AMSAssetInfoDAO.getInstance().Update(wLoginUser, wItem, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception ex) {
            wResult.FaultCode += ex.toString();
            logger.error(ex.toString());
        }

        return wResult;
    }

    /**
     * 删除单条资产
     *
     * @param wLoginUser
     * @param wItem
     * @return
     */
    @Override
    public ServiceResult<Integer> AMS_DeleteAssetInfo(BMSEmployee wLoginUser, AMSAssetInfo wItem) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);

        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);

            AMSAssetInfoDAO.getInstance().Delete(wLoginUser, wItem, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception ex) {
            wResult.FaultCode += ex.toString();
            logger.error(ex.toString());
        }

        return wResult;
    }

    /**
     * 批量删除资产
     *
     * @param wLoginUser
     * @param wIDList
     * @return
     */
    @Override
    public ServiceResult<Integer> AMS_DeleteAssetInfoList(BMSEmployee wLoginUser, List<Integer> wIDList) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);

        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);

            AMSAssetInfoDAO.getInstance().DeleteList(wLoginUser, wIDList, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception ex) {
            wResult.FaultCode += ex.toString();
            logger.error(ex.toString());
        }

        return wResult;
    }

    /**
     * 条件查询资产列表
     *
     * @param wLoginUser
     * @param wIDs
     * @param wName
     * @param wCode
     * @param wCreatorID
     * @param wActive
     * @param wStateIDList
     * @param wStandardModelID
     * @param wAssetSN
     * @param wStorageID
     * @param wKeepInUserID
     * @param wKeepInUserName
     * @param wStartTime
     * @param wEndTime
     * @param wPagination
     * @return
     */
    @Override
    public ServiceResult<List<AMSAssetInfo>> AMS_GetAssetInfoList(BMSEmployee wLoginUser,
                                                                  List<Integer> wIDs, String wName, String wCode,
                                                                  int wCreatorID, int wActive,
                                                                  List<Integer> wStateIDList,
                                                                  int wStandardModelID, String wAssetSN,
                                                                  int wStorageID, int wKeepInUserID,
                                                                  String wKeepInUserName,
                                                                  Calendar wStartTime, Calendar wEndTime,
                                                                  Pagination wPagination) {
        ServiceResult<List<AMSAssetInfo>> wResult = new ServiceResult<>();

        try {
            OutResult<Integer> wErrorCode = new OutResult<>(Integer.valueOf(0));

            wResult.Result = AMSAssetInfoDAO.getInstance().SelectList(wLoginUser,
                    wIDs, wName, wCode, wCreatorID, wActive, wStateIDList,
                    wStandardModelID, wAssetSN, wStorageID, wKeepInUserID, wKeepInUserName,
                    wStartTime, wEndTime, wPagination, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception ex) {
            wResult.FaultCode += ex.toString();
            logger.error(ex.toString());
        }

        return wResult;
    }

    //endregion

    //region 资产分类

    @Override
    public ServiceResult<AMSAssetClassification> AMS_QueryAssetClassification(BMSEmployee wLoginUser, int wID) {
        ServiceResult<AMSAssetClassification> wResult = new ServiceResult<>();
        wResult.Result = new AMSAssetClassification();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            wResult.Result = AMSAssetClassificationDAO.getInstance().SelectByID(wID, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public synchronized ServiceResult<Integer> AMS_InitAssetClassification(BMSEmployee wLoginUser) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            if (AMSAssetClassEnumInited)
                return wResult;
            OutResult<Integer> wErrorCode = new OutResult<>(0);
            //固定资产初始化
            List<EnumItem> wEnumItemList = BaseEnum.getEnumList(AMSFixedAssetsType.values());
            wEnumItemList.removeIf(p -> p.ID <= 0);

            AMSAssetClassification wAMSAssetClassification;
            Map<String, Object> wMap;
            AMSAssetClassification amsAssetClassification;
            for (EnumItem wEnumItem : wEnumItemList) {

                wMap = new HashMap<>();
                wMap.put("TypeEnum", wEnumItem.ID);

                //根据TypeEnum查询,若存在，则不创建
                amsAssetClassification =
                        ORMUtils.GetPoService(AMSAssetClassification.class).SelectPO(wLoginUser, wMap, wErrorCode);
                if (amsAssetClassification.ID > 0) {
                    continue;
                }

                wAMSAssetClassification = new AMSAssetClassification();
                wAMSAssetClassification.TypeEnum = wEnumItem.ID;
                wAMSAssetClassification.CreatorID = BMSEmployee.SysAdmin.ID;
                wAMSAssetClassification.EditorID = BMSEmployee.SysAdmin.ID;
                wAMSAssetClassification.Active = 1;
                wAMSAssetClassification.IsDefault = 1;
                wAMSAssetClassification.Name = wEnumItem.Desc;

                ORMUtils.GetPoService(AMSAssetClassification.class).UpdatePo(wLoginUser, wAMSAssetClassification,
                        wErrorCode);

                // AMSAssetClassificationDAO.getInstance().Update(wAMSAssetClassification, wErrorCode);
            }
            DMSDeviceType wDMSDeviceType = new DMSDeviceType();
            wDMSDeviceType.Name = "默认";
            wDMSDeviceType.Active = 1;
            ORMUtils.GetPoService(DMSDeviceType.class).UpdatePo(wLoginUser, wDMSDeviceType, wErrorCode);

            AMSAssetClassEnumInited = true;

        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error("AMS_InitAssetClassification", e);
        }
        return wResult;
    }


    @Override
    public ServiceResult<List<AMSAssetClassification>> AMS_QueryAssetClassificationList(BMSEmployee wLoginUser,
                                                                                        String wName, String wCode,
                                                                                        int wParentID,
                                                                                        Calendar wStartTime,
                                                                                        Calendar wEndTime,
                                                                                        int wActive,
                                                                                        Pagination wPagination) {
        ServiceResult<List<AMSAssetClassification>> wResult = new ServiceResult<>();
        wResult.Result = new ArrayList<>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);


            //固定资产初始化
            List<EnumItem> wEnumItemList = BaseEnum.getEnumList(AMSFixedAssetsType.values());
            wEnumItemList.removeIf(p -> p.ID <= 0);

            Calendar wBaseTime = Calendar.getInstance();
            wBaseTime.set(2000, Calendar.JANUARY, 1, 0, 0, 0);

            for (EnumItem wEnumItem : wEnumItemList) {

                AMSAssetClassification wAMSAssetClassification = new AMSAssetClassification();
                wAMSAssetClassification.TypeEnum = wEnumItem.ID;
                wAMSAssetClassification.CreatorID = BMSEmployee.SysAdmin.ID;
                wAMSAssetClassification.CreateTime = Calendar.getInstance();
                wAMSAssetClassification.EditorID = 0;
                wAMSAssetClassification.EditTime = wBaseTime;
                wAMSAssetClassification.Active = 1;
                wAMSAssetClassification.IsDefault = 1;
                wAMSAssetClassification.ID = 0;
                wAMSAssetClassification.Name = wEnumItem.Desc;
                wAMSAssetClassification.Code = "AT" + String.format("%03d", wEnumItem.ID);
                AMSAssetClassificationDAO.getInstance().Update(wAMSAssetClassification, wErrorCode);
            }

            wResult.Result = AMSAssetClassificationDAO.getInstance().SelectList(-1, wName, wCode,
                    wParentID, wStartTime, wEndTime, wActive, -1, wPagination, wErrorCode);

            wResult.AddFaultCode(wErrorCode);
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> AMS_UpdateAssetClassification(BMSEmployee wLoginUser,
                                                                AMSAssetClassification wAMSAssetClassification) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            List<AMSAssetClassification> amsAssetClassifications =
                    AMSAssetClassificationDAO.getInstance().SelectList(-1, "", wAMSAssetClassification.Code, -1,
                            null,
                            null, -1, -1, Pagination.getDefault(), wErrorCode);
            if (wAMSAssetClassification.ID <= 0) {
                if (amsAssetClassifications.size() > 0) {
                    wResult.FaultCode += StringUtils.Format("提示：资产编码【{0}】重复!", wAMSAssetClassification.Code);
                    return wResult;
                }

                wAMSAssetClassification.CreatorID = wLoginUser.ID;
                wAMSAssetClassification.CreateTime = Calendar.getInstance();
            } else {
                if (amsAssetClassifications.size() > 0 && amsAssetClassifications.stream().anyMatch(p -> p.ID != wAMSAssetClassification.ID)) {
                    wResult.FaultCode += StringUtils.Format("提示：资产编码【{0}】重复!", wAMSAssetClassification.Code);
                    return wResult;
                }
            }
            wAMSAssetClassification.EditorID = wLoginUser.ID;
            wAMSAssetClassification.EditTime = Calendar.getInstance();

            wResult.Result = AMSAssetClassificationDAO.getInstance().Update(wAMSAssetClassification,
                    wErrorCode);

            wResult.AddFaultCode(wErrorCode);
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> AMS_ActiveAssetClassificationList(BMSEmployee wLoginUser,
                                                                    List<AMSAssetClassification> wAMSAssetClassificationList,
                                                                    int wActive) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            for (AMSAssetClassification wAMSAssetClassification : wAMSAssetClassificationList) {
                wAMSAssetClassification.Active = wActive;
                wAMSAssetClassification.EditorID = wLoginUser.ID;
                wAMSAssetClassification.EditTime = Calendar.getInstance();
                AMSAssetClassificationDAO.getInstance().Update(wAMSAssetClassification, wErrorCode);
            }

            wResult.AddFaultCode(wErrorCode);
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> AMS_AssetClassificationDeleteList(BMSEmployee wLoginUser,
                                                                    List<AMSAssetClassification> wDataList) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            wResult = AMSAssetClassificationDAO.getInstance().DeleteList(wDataList, wErrorCode);

            wResult.AddFaultCode(wErrorCode);
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    //endregion

    //region 工装/量器具管理

    @Override
    public void AMS_InitFrockTool(BMSEmployee wLoginUser) {
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            //①根据固定资产分类，查询工装资产数据列表
            Map<String, Object> wParams = new HashMap<String, Object>();
            wParams.put("TypeEnum", AMSFixedAssetsType.Frock.getValue());
            wParams.put("Active", 1);

            List<AMSAssetStandardModel> wAMSAssetStandardModelList =
                    ORMUtils.GetPoService(AMSAssetStandardModel.class).SelectPOList(wLoginUser, wParams,
                            Pagination.getMaxSize(), wErrorCode);

            if (wAMSAssetStandardModelList.size() == 0) {
                return;
            }
            //②遍历查询工装扩展表中是否存在此工装资产
            PoService<AMSFrockTool> amsFrockToolPoService = ORMUtils.GetPoService(AMSFrockTool.class);
            Map<String, Object> wFrockMaps;
            AMSFrockTool wAMSFrockTool;
            for (AMSAssetStandardModel wAMSAssetStandardModel : wAMSAssetStandardModelList) {
                wFrockMaps = new HashMap<>();
                wFrockMaps.put("AMSAssetInfoID", wAMSAssetStandardModel.ID);
                List<AMSFrockTool> wAMSFrockToolList = amsFrockToolPoService.SelectPOList(wLoginUser, wFrockMaps,
                        Pagination.getMaxSize(), wErrorCode);
                //③若存在，则不创建
                if (wAMSFrockToolList.size() > 0) {
                    continue;
                }
                //④若不存在，则创建
                wAMSFrockTool = new AMSFrockTool();
                wAMSFrockTool.StandardModelID = wAMSAssetStandardModel.ID;
                amsFrockToolPoService.UpdatePo(wLoginUser, wAMSFrockTool, wErrorCode);
            }
        } catch (Exception ex) {
            logger.error("AMS_InitFrockTool", ex);
        }
    }

    @Override
    public void AMS_InitMeasureTool(BMSEmployee wLoginUser) {
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            //①根据固定资产分类，查询工装资产数据列表
            Map<String, Object> wParams = new HashMap<String, Object>();
            wParams.put("TypeEnum", AMSFixedAssetsType.Measure.getValue());
            wParams.put("Active", 1);

            List<AMSAssetStandardModel> wAMSAssetStandardModelList =
                    ORMUtils.GetPoService(AMSAssetStandardModel.class).SelectPOList(wLoginUser, wParams,
                            Pagination.getMaxSize(), wErrorCode);

            if (wAMSAssetStandardModelList.size() <= 0) {
                return;
            }
            //②遍历查询工装扩展表中是否存在此工装资产
            PoService<AMSMeasureTool> amsMeasureToolPoService = ORMUtils.GetPoService(AMSMeasureTool.class);
            Map<String, Object> wFrockMaps;
            AMSMeasureTool wAMSMeasureTool;
            for (AMSAssetStandardModel wAMSAssetStandardModel : wAMSAssetStandardModelList) {
                wFrockMaps = new HashMap<>();
                wFrockMaps.put("AMSAssetInfoID", wAMSAssetStandardModel.ID);
                List<AMSMeasureTool> wAMSFrockToolList = amsMeasureToolPoService.SelectPOList(wLoginUser, wFrockMaps,
                        Pagination.getMaxSize(), wErrorCode);
                //③若存在，则不创建
                if (wAMSFrockToolList.size() > 0) {
                    continue;
                }
                //④若不存在，则创建
                wAMSMeasureTool = new AMSMeasureTool();
                wAMSMeasureTool.StandardModelID = wAMSAssetStandardModel.ID;
                amsMeasureToolPoService.UpdatePo(wLoginUser, wAMSMeasureTool, wErrorCode);
            }
        } catch (Exception ex) {
            logger.error("AMS_InitFrockTool", ex);
        }
    }

    @Override
    public ServiceResult<DMSFrockLedger> DMS_QueryFrockLedgerByCode(BMSEmployee wLoginUser, String wCode) {
        ServiceResult<DMSFrockLedger> wResult = new ServiceResult<>();
        wResult.Result = new DMSFrockLedger();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            Map<String, Object> wParams = new HashMap<String, Object>();
            wParams.put("Code", wCode);
            List<DMSFrockLedger> wDMSFrockLedgerList = mDMSFrockLedgerService.SelectPOList(wLoginUser, wParams,
                    Pagination.getNewMaxSize(),
                    wErrorCode);
            if (wDMSFrockLedgerList != null && wDMSFrockLedgerList.size() > 0 && wDMSFrockLedgerList.stream().anyMatch(p -> p.Code.equals(wCode))) {
                wResult.Result = wDMSFrockLedgerList.stream().filter(p -> p.Code.equals(wCode)).findFirst().get();
            }

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    //endregion

    //region 工装台账

    @Override
    public ServiceResult<Integer> DMS_UpdateFrockLedger(BMSEmployee wLoginUser, DMSFrockLedger wDMSFrockLedger) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            AMSAssetInfo wAMSAssetInfo;
            if (wDMSFrockLedger.AssetInfoID > 0) {
                ServiceResult<AMSAssetInfo> wAssetInfoServiceResult = mAssetInfoService.SelectPO(wLoginUser,
                        wDMSFrockLedger.AssetInfoID, "");
                wAMSAssetInfo = wAssetInfoServiceResult.Result;
                if (StringUtils.isNotEmpty(wAssetInfoServiceResult.getFaultCode())) {
                    wResult.FaultCode = wAssetInfoServiceResult.getFaultCode();
                    return wResult;
                }
                if (wAMSAssetInfo == null || wAMSAssetInfo.ID <= 0) {
                    wAMSAssetInfo = new AMSAssetInfo(wDMSFrockLedger);
                } else {
                    wAMSAssetInfo.SetLedger(wDMSFrockLedger);
                }
            } else {
                wAMSAssetInfo = new AMSAssetInfo(wDMSFrockLedger);
            }
            wAMSAssetInfo.setUserInfo(wLoginUser);
            ServiceResult<Integer> wServiceResult = mAssetInfoService.UpdatePo(wLoginUser,
                    wAMSAssetInfo);

            if (StringUtils.isNotEmpty(wServiceResult.getFaultCode())) {
                wResult.FaultCode = wServiceResult.getFaultCode();
                return wResult;
            }

            wDMSFrockLedger.setUserInfo(wLoginUser);
            wDMSFrockLedger.AssetInfoID = wAMSAssetInfo.ID;
            wDMSFrockLedger.Code = wAMSAssetInfo.Code;
            wServiceResult = mDMSFrockLedgerService.UpdatePo(wLoginUser, wDMSFrockLedger);

            wResult = wServiceResult;

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> DMS_ImportFrockLedger(BMSEmployee wLoginUser, ExcelData wExcelData) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            if (wExcelData == null || wExcelData.sheetData == null || wExcelData.sheetData.size() <= 0) {
                wResult.FaultCode += "导入失败：Excel格式错误!!!";
                return wResult;
            }

            ExcelSheetData wExcelSheetData = wExcelData.sheetData.get(0);
            if (wExcelSheetData.lineData == null || wExcelSheetData.lineData.size() <= 1) {
                wResult.FaultCode += "导入失败：Excel无内容!!!";
                return wResult;
            }

            //①工装型号
            Map<String, Object> wParams = new HashMap<String, Object>();
            List<AMSFrockTool> wAMSFrockToolList = mAMSFrockToolService.SelectPOList(wLoginUser, wParams,
                    Pagination.getNewMaxSize(),
                    wErrorCode);
            if (wAMSFrockToolList == null || wAMSFrockToolList.size() <= 0) {
                wResult.FaultCode += "导入失败：未获取到工装型号信息!!!";
                return wResult;
            }
            Map<String, Integer> wNameIDGroupMap =
                    wAMSFrockToolList.stream().collect(Collectors.toMap(AMSFrockTool::getName
                            , AMSFrockTool::getID));
            Map<String, AMSFrockTool> wNameFrockToolMap =
                    wAMSFrockToolList.stream().collect(Collectors.toMap(AMSFrockTool::getName
                            , p -> p));
            //②设备列表
            List<DMSDeviceLedger> wDMSDeviceLedgerList = mDMSDeviceLedgerService.SelectPOList(wLoginUser, wParams,
                    Pagination.getNewMaxSize(),
                    wErrorCode);
            if (wDMSDeviceLedgerList == null || wDMSDeviceLedgerList.size() <= 0) {
                wResult.FaultCode += "导入失败：未获取到设备台账信息!!!";
                return wResult;
            }

            Map<String, Integer> wNameIDTypeMap =
                    wDMSDeviceLedgerList.stream().collect(Collectors.toMap(DMSDeviceLedger::getName
                            , DMSDeviceLedger::getID));
            //③班组信息
            List<BMSClassInfo> wBMSClassInfoList = mBMSClassInfoService.SelectPOList(wLoginUser, wParams,
                    Pagination.getNewMaxSize(),
                    wErrorCode);
            if (wBMSClassInfoList == null || wBMSClassInfoList.size() <= 0) {
                wResult.FaultCode += "导入失败：未获取到班组信息!!!";
                return wResult;
            }

            Map<String, Integer> wNameIDClassMap =
                    wBMSClassInfoList.stream().collect(Collectors.toMap(BMSClassInfo::getDepartmentName
                            , BMSClassInfo::getID));
            //④用户信息
            wParams.clear();
            wParams.put("Active", 1);
            List<BMSEmployee> wBMSEmployeeList = mBMSEmployeeService.SelectPOList(wLoginUser, wParams,
                    Pagination.getNewMaxSize(),
                    wErrorCode);
            if (wBMSEmployeeList == null || wBMSEmployeeList.size() <= 0) {
                wResult.FaultCode += "导入失败：未获取到用户信息!!!";
                return wResult;
            }

//            Map<String, Integer> wNameIDUserMap =
//                    wBMSEmployeeList.stream().collect(Collectors.toMap(BMSEmployee::getName
//                            , BMSEmployee::getID));

            List<DMSFrockLedger> wDMSFrockLedgerList = new ArrayList<>();
            DMSFrockLedger wDMSFrockLedger;
            for (int i = 0; i < wExcelSheetData.lineData.size(); i++) {
                if (i == 0) {
                    continue;
                }

                ExcelLineData wExcelLineData = wExcelSheetData.lineData.get(i);
                int wIndex = 0;

                //工装台账编码
                String wCol1 = wExcelLineData.colData.get(wIndex++);
                if (StringUtils.isEmpty(wCol1)) {
                    continue;
                }
                //工装台账名称
                String wCol2 = wExcelLineData.colData.get(wIndex++);
                if (StringUtils.isEmpty(wCol2)) {
                    wResult.FaultCode += StringUtils.Format("导入失败：第【{0}】行数据有误，工装台账名称不能为空!!!", i + 1);
                    return wResult;
                }
                //工装型号
                String wCol3 = wExcelLineData.colData.get(wIndex++);
                if (StringUtils.isEmpty(wCol3)) {
                    wResult.FaultCode += StringUtils.Format("导入失败：第【{0}】行数据有误，工装型号不能为空!!!", i + 1);
                    return wResult;
                }
                if (!wNameIDGroupMap.containsKey(wCol3)) {
                    wResult.FaultCode += StringUtils.Format("导入失败：第【{0}】行数据有误，工装型号信息【{1}】不存在!!!", i + 1, wCol3);
                    return wResult;
                }
                //存放位置
                String wCol4 = wExcelLineData.colData.get(wIndex++);
                //可用设备（多个以,隔开）
                String wCol5 = wExcelLineData.colData.get(wIndex++);
                List<Integer> wDeviceIDList = new ArrayList<>();
                if (StringUtils.isNotEmpty(wCol5)) {
                    if (wCol5.contains(",")) {
                        String[] splits = wCol5.split(",");
                        for (String wString : splits) {
                            if (!wNameIDTypeMap.containsKey(wString)) {
                                wResult.FaultCode += StringUtils.Format("导入失败：第【{0}】行数据有误，设备信息【{1}】不存在!!!", i + 1,
                                        wString);
                                return wResult;
                            }
                            wDeviceIDList.add(wNameIDTypeMap.get(wString));
                        }
                    } else if (wCol5.contains("，")) {
                        String[] splits = wCol5.split("，");
                        for (String wString : splits) {
                            if (!wNameIDTypeMap.containsKey(wString)) {
                                wResult.FaultCode += StringUtils.Format("导入失败：第【{0}】行数据有误，设备信息【{1}】不存在!!!", i + 1,
                                        wString);
                                return wResult;
                            }
                            wDeviceIDList.add(wNameIDTypeMap.get(wString));
                        }
                    } else {
                        if (!wNameIDTypeMap.containsKey(wCol5)) {
                            wResult.FaultCode += StringUtils.Format("导入失败：第【{0}】行数据有误，设备信息【{1}】不存在!!!", i + 1, wCol5);
                            return wResult;
                        }
                        wDeviceIDList.add(wNameIDTypeMap.get(wCol5));
                    }
                }
                //所属班组
                String wCol6 = wExcelLineData.colData.get(wIndex++);
                int wAMSClassInfoID = 0;
                if (StringUtils.isNotEmpty(wCol6)) {
                    if (!wNameIDClassMap.containsKey(wCol6)) {
                        wResult.FaultCode += StringUtils.Format("导入失败：第【{0}】行数据有误，班组信息【{1}】不存在!!!", i + 1, wCol6);
                        return wResult;
                    }
                    wAMSClassInfoID = wNameIDClassMap.get(wCol6);
                }
                //责任人
                String wCol7 = wExcelLineData.colData.get(wIndex++);
                int wDutyPersonID = 0;
                if (StringUtils.isNotEmpty(wCol7)) {
                    Optional<BMSEmployee> first =
                            wBMSEmployeeList.stream().filter(p -> p.Name.equals(wCol7)).findFirst();
                    if (!first.isPresent()) {
                        wResult.FaultCode += StringUtils.Format("导入失败：第【{0}】行数据有误，已激活的用户信息【{1}】不存在!!!", i + 1, wCol7);
                        return wResult;
                    }
                    wDutyPersonID = first.get().ID;
                }
                //备注信息
                String wCol8 = wExcelLineData.colData.get(wIndex++);

                wDMSFrockLedger = new DMSFrockLedger();
                wDMSFrockLedger.Code = wCol1;
                wDMSFrockLedger.Name = wCol2;
                wDMSFrockLedger.FrockToolID = wNameIDGroupMap.get(wCol3);
                wDMSFrockLedger.StandardModelID = wNameFrockToolMap.get(wCol3).StandardModelID;
                wDMSFrockLedger.Position = wCol4;
                wDMSFrockLedger.DeviceIDList = wDeviceIDList;
                wDMSFrockLedger.ClassInfoID = wAMSClassInfoID;
                wDMSFrockLedger.DutyPersonID = wDutyPersonID;
                wDMSFrockLedger.Description = wCol8;
                wDMSFrockLedgerList.add(wDMSFrockLedger);
            }

            //批量保存
            if (wDMSFrockLedgerList.size() > 0) {
                for (DMSFrockLedger wItem : wDMSFrockLedgerList) {
                    String wFaultCode = AMSServiceImpl.getInstance().DMS_UpdateFrockLedger(wLoginUser, wItem).FaultCode;
                    if (StringUtils.isNotEmpty(wFaultCode)) {
                        wResult.FaultCode = wFaultCode;
                        return wResult;
                    }
                }
            }

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error("DMS_ImportFrockLedger", e);
        }
        return wResult;
    }

    //endregion
}
